package com.adc.quicktools.process;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;


/**
 * @TypeName: ExecDemeanThread
 * @Package com.adc.quicktools.process
 * @Description: TODO(用一句话描述该文件做什么)
 * @author shen shenqiao2012@163.com
 * @date 2012-6-3 下午03:55:35
 */
public class ExecDaemonThread extends Thread {
	private String charset = "GBK";
	private Process process = null;
	private String cmd = null;
	private String[] evrn;
	private String dir = "";
	private String outFile;
	private int status = -1;
	private int resultvalue = -1;

	/**
	 * 
	 * @Title:
	 * @Description: TODO( 传入命令行、环境变量、指定目录、指定日志输出文件，创建守护线程 )
	 * @param cmd
	 * @param evrn
	 * @param dir
	 * @param outFile
	 */
	public ExecDaemonThread(String cmd, String[] evrn, String dir, String outFile) {
		this.cmd = cmd;
		this.evrn = evrn;
		this.dir = dir;
		this.outFile = outFile;
	}

	/**
	 * @Title:
	 * @Description: TODO( 直接初始化process，外部确定其命令及参数！ 以及输出日志文件)
	 * @param process
	 *            定义好的process
	 */
	public ExecDaemonThread(Process process, String outFile) {
		this.process = process;
		this.outFile = outFile;
	}

	/**
	 * @Title:
	 * @Description: TODO( 直接初始化process，外部确定其命令及参数！ )
	 * @param process
	 *            定义好的process
	 */
	public ExecDaemonThread(Process process) {
		this.process = process;
	}

	/**
	 * @MethodName: executeCmds
	 * @author shen
	 * @Description: TODO( 生成守护线程，并执行指定命令行 )
	 * @param cmd
	 *            命令行
	 * @param evrn
	 *            环境变量
	 * @param dir
	 *            指定路径
	 * @return void
	 */
	public void executeCmds(String cmd, String[] evrn, File dir) {
		try {
			if (cmd != null) {
				if (evrn == null && dir == null) {
					process = Runtime.getRuntime().exec(cmd);
				} else {
					if (evrn != null && dir == null) {
						process = Runtime.getRuntime().exec(cmd, evrn);
					} else if (evrn == null && dir != null) {
						process = Runtime.getRuntime().exec(cmd, null, dir);
					} else {
						process = Runtime.getRuntime().exec(cmd, evrn, dir);
					}
				}
			}
		} catch (IOException e) {
			System.out.println("process 初始化过程中出现异常！ ");
			e.printStackTrace();
		}
		PrintWriter out=null;
		InputStreamWathThread inputWathThread=null;
		ErrorInputStreamWathThread errorInputWathThread=null;
		try {
			if (process != null) {
//				inputWathThread = new InputStreamWathThread(process, charset,
//						outFile == null ? null : outFile + ".in.txt");
//				inputWathThread.start();
//				errorInputWathThread = new ErrorInputStreamWathThread(process, charset,
//						outFile == null ? null : outFile + ".err.txt");
//				errorInputWathThread.start();
				out = new PrintWriter(
						new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true);
				out.println(cmd);
				resultvalue = process.waitFor();// 等待子进程的结束，
//				inputWathThread.setOver(true);// 转换完，停止流的处理
//				errorInputWathThread.setOver(true);
//				while (true) {
//					if (inputWathThread.isStatus()&&errorInputWathThread.isStatus()) {
//						System.out.println("I/O in process has been deal!");
//						break;
//					}
//				}
				String outStr = "";
				String lsString = null;
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(process.getInputStream()));
				while ((lsString = bufferedReader.readLine()) != null) {
					System.out.println(lsString);
					outStr += lsString;
				}

			} else {
				System.out.println("process is null");
			}
		} catch (InterruptedException e) {
			System.out.println("守护线程运行时被打断，发生异常！"+e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			inputWathThread=null;
			errorInputWathThread=null;
			System.out.println("execute over! ");
			out.close();
			System.out.println(this.getClass().getName()+" returnValue is: "+resultvalue);
			if (resultvalue != 0) {
				System.out.println("process [" + process + "] 执行失败，请稍后重试！");
				status = 0;
			}
			process.destroy();
			System.gc();
		}
	}

	public void run() {
		executeCmds(cmd, evrn, new File(dir));

	}

	/**
	 * @MethodName: setCharset
	 * @author shen
	 * @Description: TODO( 设置字符集，默认为utf-8 )
	 * @param charset
	 * @return void
	 */
	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getCharset() {
		return charset;
	}

	public Process getProcess() {
		return process;
	}

	public int getResultvalue() {
		return resultvalue;
	}

	public int getStatus() {
		return status;
	}

	public String getOutFile() {
		return outFile;
	}

	public void setOutFile(String outFile) {
		this.outFile = outFile;
	}

	public String getCmd() {
		return cmd;
	}

	public String[] getEvrn() {
		return evrn;
	}

	public String getDir() {
		return dir;
	}
	
}
